<?php

namespace common\modules\pms\models;

use common\behaviors\TimestampBehavior;
use common\components\ActiveRecord;
use Yii;
use yii\db\Expression;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%pms_task}}".
 *
 * @property integer $id
 * @property integer $root
 * @property integer $lft
 * @property integer $rgt
 * @property integer $lvl
 * @property string $name
 * @property string $description
 * @property integer $project_id
 * @property integer $assigner_id
 * @property integer $start_time
 * @property integer $end_time
 * @property integer $status
 * @property integer $percentage
 * @property integer $color
 * @property string $icon
 * @property integer $icon_type
 * @property integer $active
 * @property integer $selected
 * @property integer $disabled
 * @property integer $readonly
 * @property integer $visible
 * @property integer $collapsed
 * @property integer $movable_u
 * @property integer $movable_d
 * @property integer $movable_l
 * @property integer $movable_r
 * @property integer $removable
 * @property integer $removable_all
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $created_by
 * @property integer $updated_by
 * @property string $colorClass
 */
class Task extends ActiveRecord
{

    /**
     * @const int 进行中
     */
    const STATUS_PROCESS = 2;

    /**
     * @const int 暂停中
     */
    const STATUS_SUSPEND = 3;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%pms_task}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['root', 'lft', 'rgt', 'lvl', 'project_id', 'assigner_id', 'start_time', 'end_time', 'color', 'status', 'icon_type', 'active', 'selected', 'disabled', 'readonly', 'visible', 'collapsed', 'movable_u', 'movable_d', 'movable_l', 'movable_r', 'removable', 'removable_all'], 'integer'],
            [['name'], 'required'],
            [['percentage'], 'integer'],
            [['description'], 'string'],
            [['name'], 'string', 'max' => 128],
            [['icon'], 'string', 'max' => 255]
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app/pms', 'ID'),
            'root' => Yii::t('app/pms', 'Root'),
            'lft' => Yii::t('app/pms', 'Lft'),
            'rgt' => Yii::t('app/pms', 'Rgt'),
            'lvl' => Yii::t('app/pms', 'Lvl'),
            'name' => Yii::t('app/pms', 'Name'),
            'description' => Yii::t('app/pms', 'Description'),
            'project_id' => Yii::t('app/pms', 'Project ID'),
            'assigner_id' => Yii::t('app/pms', 'Assigner ID'),
            'start_time' => Yii::t('app/pms', 'Start Time'),
            'end_time' => Yii::t('app/pms', 'End Time'),
            'color' => Yii::t('app/pms', 'Color'),
            'percentage' => Yii::t('app/pms', 'Percentage'),
            'status' => Yii::t('app/pms', 'Status'),
            'icon' => Yii::t('app/pms', 'Icon'),
            'icon_type' => Yii::t('app/pms', 'Icon Type'),
            'active' => Yii::t('app/pms', 'Active'),
            'selected' => Yii::t('app/pms', 'Selected'),
            'disabled' => Yii::t('app/pms', 'Disabled'),
            'readonly' => Yii::t('app/pms', 'Readonly'),
            'visible' => Yii::t('app/pms', 'Visible'),
            'collapsed' => Yii::t('app/pms', 'Collapsed'),
            'movable_u' => Yii::t('app/pms', 'Movable U'),
            'movable_d' => Yii::t('app/pms', 'Movable D'),
            'movable_l' => Yii::t('app/pms', 'Movable L'),
            'movable_r' => Yii::t('app/pms', 'Movable R'),
            'removable' => Yii::t('app/pms', 'Removable'),
            'removable_all' => Yii::t('app/pms', 'Removable All'),
            'created_at' => Yii::t('app/pms', 'Created At'),
            'updated_at' => Yii::t('app/pms', 'Updated At'),
            'created_by' => Yii::t('app/pms', 'Created By'),
            'updated_by' => Yii::t('app/pms', 'Updated By'),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();

        $behaviors[] = [
            'class' => TimestampBehavior::className(),
            'createdAtAttribute' => 'created_at',
            'updatedAtAttribute' => 'updated_at',
            'value' => new Expression('NOW()'),
        ];

        return $behaviors;
    }

    /**
     * {@inheritdoc}
     */
    public function beforeSave($insert)
    {
        if ( ! is_numeric($this->start_time))
        {
            $this->start_time = strtotime($this->start_time);
        }
        if ( ! is_numeric($this->end_time))
        {
            $this->end_time = strtotime($this->end_time);
        }
        return parent::beforeSave($insert);
    }

    /**
     * @return string
     */
    public function getColorClass()
    {
        foreach (self::colorArray() as $colorConfig)
        {
            if ($colorConfig['id'] == $this->color)
            {
                return $colorConfig['class'];
            }
        }
        return '';
    }

    /**
     * @inheritdoc
     */
    public static function statusArray()
    {
        return ArrayHelper::merge(parent::statusArray(), [
            self::STATUS_PROCESS => Yii::t('app/pms', 'Processing'),
            self::STATUS_SUSPEND => Yii::t('app/pms', 'Suspended'),
        ]);
    }

    /**
     * @return array
     */
    public static function colorArray()
    {
        return [
            [
                'id' => 0,
                //'name' => '<span class="label label-success">' . Yii::t('app/pms', 'Green') . '</span>',
                'name' => Yii::t('app/pms', 'Green'),
                'class' => 'ganttGreen',
            ],
            [
                'id' => 1,
                //'name' => '<span class="label label-primary">' . Yii::t('app/pms', 'Blue') . '</span>',
                'name' => Yii::t('app/pms', 'Blue'),
                'class' => 'ganttBlue',
            ],
            [
                'id' => 2,
                //'name' => '<span class="label label-warning">' . Yii::t('app/pms', 'Orange') . '</span>',
                'name' => Yii::t('app/pms', 'Orange'),
                'class' => 'ganttOrange',
            ],
            [
                'id' => 3,
                //'name' => '<span class="label label-danger">' . Yii::t('app/pms', 'Red') . '</span>',
                'name' => Yii::t('app/pms', 'Red'),
                'class' => 'ganttRed',
            ],
        ];
    }
}
